package com.tdd;

import java.math.BigDecimal;
import java.util.Stack;

public class RpnCalculator {
	private Stack<BigDecimal> values = new Stack<BigDecimal>();

	public BigDecimal peek() {
		if (values.size() > 0)
			return values.peek();
		return BigDecimal.ZERO;
	}

	public void push(BigDecimal value) {
		values.push(value);
	}

	public BigDecimal pop() {
		if (values.size() > 0)
			return values.pop();
		return BigDecimal.ZERO;
	}

	public void plus() {
		BigDecimal rhs = pop();
		BigDecimal lhs = pop();
		BigDecimal result = lhs.add(rhs);
		push(result);
	}

	public void factorial() {
		BigDecimal operand = pop();
		
		switch(BigDecimal.ZERO.compareTo(operand)) {
		case -1:
			break;
		case 0:
			push(BigDecimal.ZERO);
			return;
		case 1:
			throw new RuntimeException("No Factorial for Negative Numbes");
		}
		
		if(BigDecimal.ZERO.compareTo(operand) == 0) {
		}
		
		BigDecimal result = BigDecimal.ONE;
		while(BigDecimal.ONE.compareTo(operand) < 0) {
			result = result.multiply(operand);
			operand = operand.subtract(BigDecimal.ONE);
		}
		
		push(result);
	}

	public void minus() {
		BigDecimal rhs = pop();
		BigDecimal lhs = pop();
		BigDecimal result = lhs.subtract(rhs);
		push(result);
	}

}
